package com.chalmers.irc2you;

import java.util.LinkedList;

import com.chalmers.irc2you.model.Channel;
import com.chalmers.irc2you.model.DataHandler;
import com.chalmers.irc2you.model.Message;
import com.chalmers.irc2you.network.Connection;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainTab extends Activity {

	private LinearLayout channelList;
	private DataHandler dh;
	private SharedPreferences pref;
	private SharedPreferences.Editor editor;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintab);
		channelList = (LinearLayout) findViewById(R.id.channelListLayout);
		dh = DataHandler.getInstance(getApplicationContext());
		pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		editor = pref.edit();
		
		if (pref.getBoolean("started", false) == false) {
			showSetupDialog();
		}
		
		updateChannelList();
	}
	
	private void showSetupDialog() {
		final Dialog dialog = new Dialog(this);

		dialog.setContentView(R.layout.custom_dialog);
		dialog.setTitle("Setup");
		dialog.setCancelable(false);
		((EditText) dialog.findViewById(R.id.hostname)).setText("hubben.chalmers.it");
		((EditText) dialog.findViewById(R.id.port)).setText("4567");
		((EditText) dialog.findViewById(R.id.username)).setText("printz");
		((EditText) dialog.findViewById(R.id.nick)).setText("Printztester");

		Button dialogButton = (Button) dialog.findViewById(R.id.setupbutton);
		dialogButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				// Check so that ip, port and username are entered.
				if (!((EditText) dialog.findViewById(R.id.hostname)).getText()
						.toString().equals("")
						&& !((EditText) dialog.findViewById(R.id.port))
								.getText().toString().equals("")
						&& !((EditText) dialog.findViewById(R.id.username))
								.getText().toString().equals("")) {

					editor.putString("ip", ((EditText) dialog
							.findViewById(R.id.hostname)).getText().toString());
					editor.putString("port", ((EditText) dialog
							.findViewById(R.id.port)).getText().toString());
					editor.putString("user", ((EditText) dialog
							.findViewById(R.id.username)).getText().toString());
					editor.putString("nick", ((EditText) dialog
							.findViewById(R.id.nick)).getText().toString());
					editor.putString("defaultchannel", ((EditText) dialog
							.findViewById(R.id.defaultchannel)).getText()
							.toString());
					editor.putBoolean("notify", ((CheckBox) dialog
							.findViewById(R.id.notify)).isChecked());
					editor.commit();

					Connection.getInstance(getApplicationContext());
					Connection.connect();
					checkNotify();
					
					editor.putBoolean("started", true);
					editor.commit();
					
					dialog.dismiss();
				}
			}
		});
		dialog.show();
	}
	
	private void checkNotify() {
		if (pref.getBoolean("notify", false) != false) {
			Intent intent = new Intent(getApplicationContext(),
					Irc2YouNotificationManager.class);
			startService(intent);
		}
	}
	
	public void onPause(){
		editor.commit();
		super.onPause();
	}
	
	public void onDestroy(){
		editor.commit();
		super.onDestroy();
	}
	
	private void updateChannelList(){
		LinkedList<Channel> tempChannels = dh.getAllChannels();
		for (int i = 0; i < tempChannels.size(); i++) {
			final ChannelView view = new ChannelView(getApplicationContext(),
					tempChannels.get(i).getName(), tempChannels.get(i)
							.isRead());
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					dh.getChannel(view.getName());
				}
			});
			channelList.addView(view);
		}
	}
}
